<?php
namespace Core\Security;

use Modules\User\Entity\User as UserEntity;
use Phalcon\Mvc\User\Plugin;

class User extends Plugin
{
    protected $_id;
    protected $_name;
    protected $_face = '/themes/AdminLTE/dist/img/user2-160x160.jpg';
    protected $_contentModel = ['anonymous' => 'anonymous'];
    protected $_loginTime;
    protected $_isLogin = false;
    protected $_admin = false;
    protected $_field = ['name', 'contentModel', 'face'];

    public function __construct()
    {
        $di = $this->getDI();
        $session = $di->getShared('session');
        $userSession = $session->has('user') ? $session->get('user') : [];
        if ($userSession || !empty($userSession)) {
            $this->init($userSession);
        }
    }

    public function getRoles()
    {
        return $this->_contentModel;
    }

    public function logout()
    {
        $this->getDI()->getShared('session')->remove('user');
        $this->_isLogin = false;
        $this->_admin = false;
        $this->_face = '/themes/AdminLTE/dist/img/user2-160x160.jpg';
        $this->_contentModel = ['anonymous' => 'anonymous'];
    }

    public function isLogin()
    {
        return $this->_isLogin;
    }

    protected function init($user)
    {
        foreach ($user as $key => $value) {
            $this->{$key} = $value;
        }

        $this->_isLogin = true;

        if (isset($user['contentModel']['admin'])) {
            $this->_admin = true;
        }
    }

    public function login($user)
    {
        $output = [];

        if ($user instanceof UserEntity) {
            $uid = (string) $user->getId();
            $user = $user->toArray();
            $user = objectToArray($user);
            $user['id'] = $uid;
        }

        foreach ($this->_field as $name) {
            if (!isset($user[$name])) {
                return false;
            }
            $item = $user[$name];
            if (is_object($item)) {
                $item = (array) $item;
            }
            $this->{'_' . $name} = $item;
            $output['_' . $name] = $item;
        }
        $output['_id'] = $user['id'];

        $this->_isLogin = true;

        $contentModel = [];
        if (isset($output['_contentModel']) && is_array($output['_contentModel'])) {
            foreach ($output['_contentModel'] as $role) {
                $contentModel[$role] = $role;
            }
            $output['_contentModel'] = $contentModel;
        }

        if (isset($output['_contentModel']['contentModel']['admin'])) {
            $this->_admin = true;
        }

        $this->_loginTime = time();

        $this->getDI()->getShared('session')->set('user', $output);

        return true;
    }

    public function isAdmin()
    {
        return $this->_admin;
    }

    public function getUid()
    {
        if ($this->isLogin()) {
            return (string) $this->_id;
        } else {
            return [
                'ip' => $this->getDI()->getRequest()->getClientAddress(),
                'browser' => getBroswer(),
                'os' => getOs(),
            ];
        }
    }

    public function updateItem($info)
    {
        $di = $this->getDI();
        $session = $di->getShared('session');
        $userSession = $session->has('user') ? $session->get('user') : [];
        $userSession = array_merge($userSession, $info);
        if ($userSession && !empty($userSession)) {
            $this->init($userSession);
        }
        $this->getDI()->getShared('session')->set('user', $userSession);
    }

    public function __get($name)
    {
        $realName = '_' . $name;
        if (isset($this->{$realName})) {
            return $this->{$realName};
        }
    }
}
